﻿package com.scn.base.tags;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.scn.base.domain.MenuFuncVO;
import com.scn.base.service.ISysFuncService;

public class FunctionTag extends TagSupport { 
	private static Logger  log = Logger.getLogger(ContextPathTag.class);
	
	private String funcs;
	private String smid;
	public String getSmid() {
		return smid;
	}

	public void setSmid(String smid) {
		this.smid = smid;
	}

	public String getFuncs() {
		return funcs;
	}

	public void setFuncs(String funcs) {
		this.funcs = funcs;
	}

	/**
	 * tlist:header 标签开始
	 *
	 * @exception JspException
	 */
	public int doStartTag() throws JspException {
	  return SKIP_BODY;
	}

	/**
	 * 从session中读取错误列表
	 *
	 * @exception JspException
	 */
	public int doEndTag() throws JspException
	{
	  JspWriter writer = pageContext.getOut();
	  HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
	  ServletContext context = request.getSession().getServletContext();
	  ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
	  ISysFuncService sysFuncService=(ISysFuncService)ctx.getBean("sysFuncService");
	  if(funcs==null||funcs.trim().equals("")){
		  return EVAL_PAGE;
	  }
	  String[] func=funcs.split("\\;");
	  String temp="";
	  for(int t=0;t<func.length;t++){
		  temp+="'"+func[t]+"',";
	  }
	  temp="("+temp.substring(0,temp.length()-1)+")";
	  Map param=new HashMap();
	  param.put("smid",smid);
	  param.put("incode",temp);
	  List alist=sysFuncService.selectFuncByMenu(param);
	  String strReturn="";
	  if(alist==null){
		  try {
				writer.write(strReturn);
		  } catch (IOException e) {
			e.printStackTrace();
		  }
		  return EVAL_PAGE;
	  }
	  for(int t=0;t<alist.size();t++){
		  MenuFuncVO sfvo=(MenuFuncVO)alist.get(t);
		  strReturn+="FUNC"+sfvo.getSmfcode()+";";
	  }
	  try {
		writer.write(strReturn);
	  } catch (IOException e) {
		e.printStackTrace();
	  }
	  return EVAL_PAGE;
	}

}